package com.seadragon.app.dms.model;

import static javax.persistence.GenerationType.IDENTITY;

import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.UniqueConstraint;

import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

@Entity
@Table(name="CUSTOMER",uniqueConstraints = {@UniqueConstraint(columnNames = {"USER_NAME"}), @UniqueConstraint(columnNames = {"TOKEN"})})
public class Customer extends Resident {
	/**
	 * Generated UID
	 */
	private static final long serialVersionUID = 6436641609670502370L;

	@Id
	@Column(name = "ID")
	@GeneratedValue(strategy = IDENTITY)
	private long id;

	@OneToMany(orphanRemoval=true, mappedBy="customer", cascade=CascadeType.ALL, fetch=FetchType.EAGER)
	@Fetch(FetchMode.JOIN)
	private Set<CreditCard> creditCards;
	
	@OneToMany(mappedBy="customer")
	private Set<Order> orders;

	@Column(name="USER_NAME")
	private String userName;
	
	@Column(name="PASSWORD")
	private String password;
	
	@Column(name="STATUS")
	private int status;
	
	@Column(name="TIME_TO_EXPIRE")
	@Temporal(TemporalType.TIME)
	private Date timeToExpire;
	
	@Column(name="TOKEN")
	private String token;
	
	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public Set<CreditCard> getCreditCards() {
		return creditCards;
	}

	public void setCreditCards(Set<CreditCard> creditCards) {
		this.creditCards = creditCards;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	
	public Set<Order> getOrders() {
		return orders;
	}

	public void setOrders(Set<Order> orders) {
		this.orders = orders;
	}

	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	public String getName(){
		return getFirstName() + " " + getLastName();
	}

	public Date getTimeToExpire() {
		return timeToExpire;
	}

	public void setTimeToExpire(Date timeToExpire) {
		this.timeToExpire = timeToExpire;
	}

	public String getToken() {
		return token;
	}

	public void setToken(String token) {
		this.token = token;
	}
	
	public boolean addCreditCard(CreditCard cc){
		if (this.creditCards==null){
			this.creditCards = new HashSet<CreditCard>();
		}
		if (cc!=null)
			cc.setCustomer(this);
		return this.creditCards.add(cc);
	}
	
	public CreditCard getCreditCardByAccountNumber(String accountNumber){
		if(accountNumber == null || accountNumber.length() == 0){
			return null;
		}
		if(creditCards == null || creditCards.size() == 0){
			return null;
		}
		Iterator<CreditCard> iter = creditCards.iterator();
		while(iter.hasNext()){
			CreditCard cc = iter.next();
			if(cc.getAccountNumber().equalsIgnoreCase(accountNumber)){
				return cc;
			}
		}
		return null;
	}
	
	public CreditCard getCreditCardById(Long id){
		if(id == null){
			return null;
		}
		if(creditCards == null || creditCards.size() == 0){
			return null;
		}
		Iterator<CreditCard> iter = creditCards.iterator();
		while(iter.hasNext()){
			CreditCard cc = iter.next();
			if(cc.getId()==id){
				return cc;
			}
		}
		return null;
	}
}
